package com.zys.el.user.client;

import com.zys.el.time.entity.Deal;
import com.zys.el.time.vojo.Result;
import feign.hystrix.FallbackFactory;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;


@RequestMapping(value = "/deal")
@FeignClient(name = "time-service", /*fallback = DealClient.DealClientFallback.class*/fallbackFactory = DealClient.DealClientFallbackFactory.class)
public interface DealClient {

    @ApiOperation(value = "加载", notes = "根据ID加载")
    @GetMapping("/list")
    public Result<List<Deal>> list(@RequestParam("userId") String userId);

    @GetMapping("/get/{id}")
    public Deal getDealById(@PathVariable("id") String id);


    @Component
    @RequestMapping("/time-fallback")
    class DealClientFallback implements DealClient {
        private static final Logger LOGGER = LoggerFactory.getLogger(DealClient.DealClientFallback.class);

        @Override
        public Result<List<Deal>> list(@RequestParam("userId") String userId) {
            
            LOGGER.error("异常出现，进入fallback方法");
            return null;
        }

        @Override
        public Deal getDealById(String id) {
            LOGGER.error("异常出现，进入fallback方法");
            return null;
        }
    }

    @Component
    @RequestMapping("/deal-fallback-factory")
    class DealClientFallbackFactory implements FallbackFactory<DealClient> {


        Logger LOGGER = LoggerFactory.getLogger(DealClientFallbackFactory.class);

        @Override
        public DealClient create(Throwable throwable) {
            throwable.printStackTrace();
            LOGGER.error(throwable.getMessage());
            return new DealClient() {
                @Override
                public Result<List<Deal>> list(String userId) {
                    LOGGER.error("异常出现，进入fallback方法");
                    return null;
                }

                @Override
                public Deal getDealById(String id) {
                    LOGGER.error("异常出现，进入fallback方法");
                    return null;
                }
            };
        }
    }

}